package com.ruoyi.web.controller.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.controller.demo.domain.CarList;
import com.ruoyi.web.controller.mapper.CarUseRecordMapper;
import com.ruoyi.web.controller.demo.domain.CarUseRecord;
import com.ruoyi.web.controller.service.ICarUseRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 使用记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-26
 */
@Service
public class CarUseRecordServiceImpl implements ICarUseRecordService 
{
    @Autowired
    private CarUseRecordMapper carUseRecordMapper;

    /**
     * 查询使用记录
     * 
     * @param recordId 使用记录主键
     * @return 使用记录
     */
    @Override
    public CarUseRecord selectCarUseRecordByRecordId(Long recordId)
    {
        return carUseRecordMapper.selectCarUseRecordByRecordId(recordId);
    }

    /**
     * 查询使用记录列表
     * 
     * @param carUseRecord 使用记录
     * @return 使用记录
     */
    @Override
    public List<CarUseRecord> selectCarUseRecordList(CarUseRecord carUseRecord)
    {
        return carUseRecordMapper.selectCarUseRecordList(carUseRecord);
    }

    /**
     * 新增使用记录
     * 
     * @param carUseRecord 使用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCarUseRecord(CarUseRecord carUseRecord)
    {
        int rows = carUseRecordMapper.insertCarUseRecord(carUseRecord);
        insertCarList(carUseRecord);
        return rows;
    }

    /**
     * 修改使用记录
     * 
     * @param carUseRecord 使用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCarUseRecord(CarUseRecord carUseRecord)
    {
//        carUseRecordMapper.deleteCarListByCarId(carUseRecord.getRecordId());
        insertCarList(carUseRecord);
        return carUseRecordMapper.updateCarUseRecord(carUseRecord);
    }

    /**
     * 批量删除使用记录
     * 
     * @param recordIds 需要删除的使用记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarUseRecordByRecordIds(String recordIds)
    {
//        carUseRecordMapper.deleteCarListByCarIds(Convert.toStrArray(recordIds));
        return carUseRecordMapper.deleteCarUseRecordByRecordIds(Convert.toStrArray(recordIds));
    }

    /**
     * 删除使用记录信息
     * 
     * @param recordId 使用记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarUseRecordByRecordId(Long recordId)
    {
//        carUseRecordMapper.deleteCarListByCarId(recordId);
        return carUseRecordMapper.deleteCarUseRecordByRecordId(recordId);
    }

    /**
     * 新增车辆列信息
     * 
     * @param carUseRecord 使用记录对象
     */
    public void insertCarList(CarUseRecord carUseRecord)
    {
        List<CarList> carListList = carUseRecord.getCarListList();
        Long recordId = carUseRecord.getRecordId();
        if (StringUtils.isNotNull(carListList))
        {
            List<CarList> list = new ArrayList<CarList>();
            for (CarList carList : carListList)
            {
                carList.setCarId(recordId);
                list.add(carList);
            }
            if (list.size() > 0)
            {
                carUseRecordMapper.batchCarList(list);
            }
        }
    }
}
